package com.shupian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shupian.constants.SystemConstants;
import com.shupian.domain.ResponseResult;
import com.shupian.domain.dto.LinkAddDto;
import com.shupian.domain.entity.Link;
import com.shupian.domain.entity.User;
import com.shupian.domain.vo.LinkVo;
import com.shupian.domain.vo.PageVo;
import com.shupian.exception.SystemException;
import com.shupian.mapper.LinkMapper;
import com.shupian.service.LinkService;
import com.shupian.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2022-05-23 11:02:17
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Autowired
    private LinkMapper linkMapper;

    @Override
    public ResponseResult getAllLink() {
        //查询所有通过的
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus, SystemConstants.Link_STATUS_NORMAL);
        List<Link> links = list(queryWrapper);
        //分装返回
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(links, LinkVo.class);
        return ResponseResult.okResult(linkVos);
    }

    @Override
    public ResponseResult getAllLinkPage(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name),Link::getName,name)
                .eq(StringUtils.hasText(status),Link::getStatus,status);
        Page<Link> page = new Page<>(pageNum,pageSize);
        page(page, queryWrapper);
        List<Link> links = page.getRecords();
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(links, LinkVo.class);

        return ResponseResult.okResult(new PageVo(linkVos,page.getTotal()));
    }

    @Override
    public ResponseResult addLink(LinkAddDto linkAddDto) {
        if(exist(linkAddDto.getName(),null,null)){
            throw new SystemException(508,"名字已存在");
        }
        if (exist(null,null,linkAddDto.getAddress())){
            throw new SystemException(509, "地址已存在");
        }
        Link link = BeanCopyUtils.copyBean(linkAddDto, Link.class);
        save(link);
        return ResponseResult.okResult();
    }

    private boolean exist(String name, String description, String address) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(name), Link::getName, name);
        queryWrapper.eq(StringUtils.hasText(description), Link::getDescription, description);
        queryWrapper.eq(StringUtils.hasText(address), Link::getAddress, address);
        return count(queryWrapper) > 0;
    }
}

